package com.hnzq.cloud.driversend.controller;

import com.hnzq.cloud.IO.AddDriverworkIO;
import com.hnzq.cloud.IO.AddSendOrderIO;
import com.hnzq.cloud.IO.DelOrderForSendIO;
import com.hnzq.cloud.IO.EditSendOrderIO;
import com.hnzq.cloud.IO.entity.FreDriverwork;
import com.hnzq.cloud.IO.entity.FreSend;
import com.hnzq.cloud.OrderAPI;
import com.hnzq.cloud.api.FreDriverworkApi;
import com.hnzq.cloud.api.FreSendOrderApi;
import com.hnzq.cloud.api.SiteQueryApi;
import com.hnzq.cloud.driversend.exception.SendException;
import com.hnzq.cloud.driversend.param.AddToSendParam;
import com.hnzq.cloud.driversend.param.CreateSendParam;
import com.hnzq.cloud.driversend.param.DelToSendParam;
import com.hnzq.cloud.driversend.param.SendToDriverParam;
import com.hnzq.cloud.dto.OneSiteDto;
import com.hnzq.cloud.model.Result;
import com.hnzq.cloud.param.JudgeOrderIdsParam;
import com.hnzq.cloud.param.UpdateOrderListStatusParam;
import com.hnzq.cloud.param.UpdateOrderStatusParam;
import com.hnzq.cloud.util.ComUtil;
import com.hnzq.cloud.util.FreException;
import io.seata.spring.annotation.GlobalTransactional;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("warehouseSend")
public class WarehouseController {
    @Autowired
    private FreSendOrderApi freSendOrderApi;
    @Autowired
    private FreDriverworkApi freDriverworkApi;
    @Autowired
    private OrderAPI orderAPI;

    //创建配送单
    @PostMapping("createSend")
    public Result createSend(@RequestBody CreateSendParam createSendParam) throws FreException {
        JudgeOrderIdsParam judgeOrderIdsParam = new JudgeOrderIdsParam();
        judgeOrderIdsParam.setOrderIds(createSendParam.getOrderIds());
        judgeOrderIdsParam.setOrderStatus(1);
        if (orderAPI.judgeOrderIds(judgeOrderIdsParam).getCode()!=200) {
            throw new FreException("存在不可选取的订单",500);
        }
//        UpdateOrderListStatusParam updateOrderListStatusParam = new UpdateOrderListStatusParam();
//        updateOrderListStatusParam.setOrderIds(createSendParam.getOrderIds());
//        updateOrderListStatusParam.setOrderStatus(2);
//        orderAPI.updateOrderListStatus(updateOrderListStatusParam);
        AddSendOrderIO addSendOrderIO = ComUtil.copyProperties(createSendParam, AddSendOrderIO.class);
        Result result = freSendOrderApi.addSendOrder(addSendOrderIO);
        if (result.getCode()!=200){
            throw new FreException("添加失败",500);
        }
        return result;

    }
    //添加订单到配送单
    @PutMapping("addOrderToSend")
    public Result addToSend(@RequestBody AddToSendParam addToSendParam) throws FreException {
        JudgeOrderIdsParam judgeOrderIdsParam = new JudgeOrderIdsParam();
        List<Integer> orderIds = addToSendParam.getOrderIds();
        judgeOrderIdsParam.setOrderIds(orderIds);
        judgeOrderIdsParam.setOrderStatus(1);
        if (orderAPI.judgeOrderIds(judgeOrderIdsParam).getCode()!=200) {
            throw new FreException("存在不可选取的订单",500);
        }
        AddSendOrderIO addSendOrderIO = new AddSendOrderIO();
        addSendOrderIO.setSendId(addToSendParam.getSendId());
        addSendOrderIO.setSendWeight(addToSendParam.getSendWeight());
        addSendOrderIO.setOrderIds(judgeOrderIdsParam.getOrderIds());
        if (freSendOrderApi.addToSend(addSendOrderIO).getCode()!=200) {
            throw new FreException("添加订单失败",500);

        }
//        UpdateOrderListStatusParam updateOrderListStatusParam = new UpdateOrderListStatusParam();
//        updateOrderListStatusParam.setOrderIds(addToSendParam.getOrderIds());
//        updateOrderListStatusParam.setOrderStatus(2);
//        orderAPI.updateOrderListStatus(updateOrderListStatusParam);
        return Result.newInstance().setCode(200).setMessage("添加成功！");
    }

    @PutMapping("delOrderToSend")
    public Result delToSend(@RequestBody DelToSendParam delToSendParam) throws FreException {
        JudgeOrderIdsParam judgeOrderIdsParam = new JudgeOrderIdsParam();
        List<Integer> orderIds = delToSendParam.getOrderIds();
        judgeOrderIdsParam.setOrderIds(orderIds);
        judgeOrderIdsParam.setOrderStatus(1);
        if (orderAPI.judgeOrderIds(judgeOrderIdsParam).getCode()!=200) {
            throw new FreException("存在不可选取的订单",500);
        }
        DelOrderForSendIO delOrderForSendIO = new DelOrderForSendIO();
        delOrderForSendIO.setSendId(delToSendParam.getSendId());
        delOrderForSendIO.setOrderIds(judgeOrderIdsParam.getOrderIds());
        if (freSendOrderApi.deleteForSend(delOrderForSendIO).getCode()!=200) {
            throw new FreException("删除订单失败",500);

        }
//        UpdateOrderListStatusParam updateOrderListStatusParam = new UpdateOrderListStatusParam();
//        updateOrderListStatusParam.setOrderIds(addToSendParam.getOrderIds());
//        updateOrderListStatusParam.setOrderStatus(2);
//        orderAPI.updateOrderListStatus(updateOrderListStatusParam);
        return Result.newInstance().setCode(200).setMessage("删除成功！");
    }


    /**
    * @作者: tiangle
    * @描述：传入sendid和司机的userId，将send分配给司机
    * @时间：2022/2/24 17:51
    * @参数类型:[sendToDriverParam]
    * @返回：com.hnzq.cloud.model.Result
    **/
    //分配配送单给司机
    @GlobalTransactional
    @PutMapping("driverGet")
    public Result driverGet(@RequestBody SendToDriverParam sendToDriverParam) throws FreException {

        Result<FreSend> byId = freSendOrderApi.getById(sendToDriverParam.getSendId());
        if (byId.getCode()!=200){
            throw new SendException(byId.getMessage(),500);
        }
        System.out.println(byId.getData());
//        Result<List<Integer>> orders = freSendOrderApi.getOrders(sendToDriverParam.getSendId());
//        UpdateOrderListStatusParam updateOrderListStatusParam = new UpdateOrderListStatusParam();
//        updateOrderListStatusParam.setOrderIds(orders.getData());
//        updateOrderListStatusParam.setOrderStatus(2);
//        orderAPI.updateOrderListStatus(updateOrderListStatusParam);
        EditSendOrderIO editSendOrderIO = ComUtil.copyProperties(byId.getData(),EditSendOrderIO.class);
        Result<FreDriverwork> byArea = freDriverworkApi.getByArea(byId.getData().getAreaId());


        if (byArea.getCode()!=200||byArea.getData().getDriverStatus()!=0){
            throw new FreException("不能分配该订单给这个司机",500);
        }
        editSendOrderIO.setDriverId(byArea.getData().getDriverId());
        System.out.println(editSendOrderIO);
        if (freSendOrderApi.editSendOrder(editSendOrderIO).getCode()!=200) {
            throw new SendException("订单分配失败",500);
        }

        AddDriverworkIO build1 = AddDriverworkIO.builder().driverId(byArea.getData().getDriverId()).driverStatus(2).build();
        if (freDriverworkApi.editDriverworkStatus(build1).getCode()!=200) {
            throw new SendException("司机分配失败，请稍后重试",500);

        }

        return Result.newInstance().setMessage("分配成功").setCode(200);
    }

}
